package net.lainiao.mycost.controller;

import net.lainiao.mycost.bean.MenuInfo;
import net.lainiao.mycost.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by Administrator on 2017/3/22.
 */
@Controller
@RequestMapping("/menu/")
public class MenuController extends Page {
    @RequestMapping("menuleft.do")
    public String menuleft(Model model, MenuInfo menuInfo){
        List<MenuInfo> list=menuInfoService.list(menuInfo);
        model.addAttribute("list",list);
        return "menu/menu_left";
    }

    @RequestMapping("menumain.do")
    public String menumain(MenuInfo menuInfo,Model model){
        MenuInfo info=null;
        if(menuInfo!=null&&menuInfo.getMenuId()!=null){
            if(menuInfo.getMenuId()==0){
                info=new MenuInfo();
                info.setMenuId(0);
                info.setMenuName("根节点");
                info.setParentMenuId(-1);
            }else{
                info=menuInfoService.getInfo(menuInfo);
            }
        }
        model.addAttribute("menu",info);
        return "menu/menu_main";
    }

    @RequestMapping("list.do")
    public String list(){
        return "menu/menu_index";
    }

    @RequestMapping("menutop.do")
    public String menutop(){
        return "menu/menu_top";
    }

    @RequestMapping("loadadd.do")
    public String loadadd(MenuInfo info,Model model){
        List<MenuInfo> list=menuInfoService.list(info);
        model.addAttribute("list",list);
        model.addAttribute("menu",info);
        return "menu/menu_add";
    }

    @RequestMapping("add.do")
    public String add(MenuInfo info,Model model){
        menuInfoService.add(info);
        return "menu/menu_info";
    }

    @RequestMapping("loadupdate.do")
    public String loadupdate(MenuInfo menuInfo,Model model){
        List<MenuInfo> list=menuInfoService.list(menuInfo);
        model.addAttribute("list",list);
        MenuInfo info=menuInfoService.getInfo(menuInfo);
        model.addAttribute("menu",info);
        return "menu/menu_update";
    }

    @RequestMapping("update.do")
    public String update(MenuInfo info,Model model){
        menuInfoService.update(info);
        return  "menu/menu_info";
    }

    @RequestMapping("delete.do")
    public String delete(MenuInfo info,Model model){
        List<MenuInfo> list=menuInfoService.getMenuList(info.getMenuId());
        if(list!=null&&list.size()>0){
            model.addAttribute("info","对不起，请删除当前菜单的子菜单");
        }else{
            menuInfoService.delete(info);
            model.addAttribute("info","当前菜单删除成功");
        }
        return "menu/menu_info";
    }
}
